export module globalModule {
	export var oid: any = 2;
}

function req(obj: any) {
	return new Promise((resolve, reject) => {
		
		// const HOST = 'http://cdb1.utools.club/'
		// const HOST = 'http://192.168.1.117:8981/'// 本地地址
		const HOST = 'http://cdb.fengzhuan.org/'  // 线上地址   更换时登录 注册  和忘记密码  需要改o单id=2

		//  const HOST = 'http://1t9294g931.iok.la:59400/'

		// const HOST = 'http://cdb.lianmengxiton.cn/'


		var method = obj.method || "GET";
		var url = HOST + obj.url || "";
		if(obj.url.indexOf('http') != -1){
			url = obj.url;
		}
		var data = obj.data || {};
		var header = obj.header || {
			'Content-Type': obj.contentType || 'application/json',
			'token': uni.getStorageSync('token'),
			'appType': 'OPEN'
		};
		var success = obj.success; // 成功回调函数
		var fail = obj.fail; //表示失败后，要执行的回调函数

		uni.request({
			url: url,
			data: data,
			method: method,
			header: header,
			success: ((res: any) => {
				uni.showLoading({
					title: '加载中'
				});

				if (res.statusCode === 403 || res.statusCode === 401) {
					// 错误处理，返回登录页
					uni.reLaunch({ url: '/pages/login/index' })
				}
				else if (res.data.message === '用户未登录,请先登录程序') {
					uni.showToast({
						title: '用户登陆状态失效',
						icon: 'none',
						duration: 2000,
						success: function (res) {
							setTimeout(() => {
								uni.reLaunch({ url: '/pages/login/index' })
							}, 1000)
						}
					});
				}
				else if (res.statusCode === 200) {
					resolve(res)
					uni.hideLoading();
				}
				else if (res.data.code == 404) {
					if(res.data.message==null){
						var tip:any='后台错误(尝试下拉刷新或联系管理员)'
					}else{
						var tip:any=res.data.message
					}
					uni.showToast({
						title: tip,
						icon: 'none',
						duration: 2000
					});
				}
			}),
			fail: ((err) => {
				reject(err)
			})
		})
	})
}
export default req